package com.anming.cat.web;

import com.anming.cat.util.RedisLockUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestLockController {
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("test")
    public String test(@RequestParam("id") String id){
        String lockId = RedisLockUtils.
                lockFailThrowException(redisTemplate, "addCustomerAdviser", 40);
        System.out.println(id+"加上锁了----------------");
        try {
            Thread.sleep(40000);

            return "test hello";
        }catch (Exception e){
            return "error";
        }finally {

            RedisLockUtils.unlock(redisTemplate, "addCustomerAdviser", lockId);
            System.out.println(id+"释放锁了。。。。。。。。。");
        }
    }
}
